package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2023/9/13 09:44
 * @apiNote
 */

public class Test00075SortColors {
    public static void test(int[] nums) {
        int n  = nums.length;
        int ptr = 0;
        for(int i = 0; i < n; i++){
            if(nums[i] == 0){
                swap(nums,i,ptr);
                ptr++;
            }
        }
        for(int i = ptr; i < n; i++){
            if(nums[i] == 1) {
                swap(nums, i, ptr);
                ptr++;
            }
        }
    }

    public static void test2(int[] nums) {
        int n = nums.length;
        int p0 = 0;
        int p1 = 0;
        for(int i = 0 ;i<n;i++){
            if(nums[i] == 1){
                swap(nums,i,p1);
                p1++;
            }else if(nums[i] == 0){
                swap(nums,i,p0);
                if(p0<p1){
                    swap(nums,i,p1);
                }
                p0++;
                p1++;
            }
        }
    }


    public static void test3(int[] nums) {
        int n = nums.length;
        int p0 = 0;
        int p2 = n-1;
        for(int i = 0 ;i<=p2;i++){
            while(i<=p2 && nums[i] == 2){
                swap(nums,i,p2);
                p2--;
            }
            if(nums[i] == 0){
                swap(nums,i,p0);
                p0++;
            }
        }
    }

    public static void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}
